<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <meta http-equiv="X-UA-Compatible" content="ie=edge">
   <script src="../js/jquery-1.11.3.min.js"></script>
   <title>上传文件</title>
</head>
<body>
   <input type="file" id="uploadimg">
   <button>上传</button>
   <img src="" alt="" id="img">
   <script>
   //上传文件只能是post,
    //用FormData
    $('button').on('click',function(){
       var $uploadImg=$('#uploadimg')
      //得到一个formData实例
      var formData = new FormData()
      //把文件内容存放在这个实例中
      formData.append('img',$uploadImg[0].files[0])//取到input的原生对象，用files属性取到文件
      $.ajax({
         url:'/api/v1/upload',
         method:'POST',
         data:formData,
         //告诉jquery不要处理数据，就提交formData本身
         processData:false,
         //告诉jquery不判断类型
         contentType:false,
         success:resp=>{
            console.log(resp)
            $('#img').css('display','block').attr('src',resp.src)
         },
         dataType:'json'
      })
    })
   </script>
</body>
</html>